package com.qf.oa_system.service.impl;

import com.qf.oa_system.entity.*;
import com.qf.oa_system.mapper.PermissionsDao;
import com.qf.oa_system.service.PermissionsService;
import com.qf.oa_system.service.RolesService;
import com.qf.oa_system.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Ȩ?ޱ(Permissions)表服务实现类
 *
 * @author makejava
 * @since 2020-06-03 20:26:24
 */
@Service("permissionsService")
public class PermissionsServiceImpl implements PermissionsService {
    @Resource
    private PermissionsDao permissionsDao;

    @Resource
    private RolesService rolesService;

    @Resource
    private UserService userService;

    /**
     * 通过ID查询单条数据
     *
     * @param permId 主键
     * @return 实例对象
     */
    @Override
    public Permissions queryById(Integer permId) {
        return this.permissionsDao.queryById(permId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Permissions> queryAllByLimit(int offset, int limit) {
        return this.permissionsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param permissions 实例对象
     * @return 实例对象
     */
    @Override
    public Permissions insert(Permissions permissions) {
        this.permissionsDao.insert(permissions);
        return permissions;
    }

    /**
     * 修改数据
     *
     * @param permissions 实例对象
     * @return 实例对象
     */
    @Override
    public Permissions update(Permissions permissions) {
        this.permissionsDao.update(permissions);
        return this.queryById(permissions.getPermId());
    }

    /**
     * 通过主键删除数据
     *
     * @param permId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer permId) {
        return this.permissionsDao.deleteById(permId) > 0;
    }

    @Override
    public Set<String> listPermissions(String userName) {
        //获得当前用户的所有角色实体
        Set<String> permissions = permissionsDao.getPermissions(userName);
        return permissions;
    }
}